Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 11-Oct-2010 (Priyabrata)

Public Class frmRevenueReport

    Private mstrReportType As String

    Public Sub New(ByVal strRerportType As String)

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        mstrReportType = strRerportType
    End Sub

    Private Sub frmRevenueReport_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmRevenueReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call PopulateLookUpName()

            dteFromDate.Properties.MaxValue = Today
            dteToDate.Properties.MaxValue = Today
            dteMnthFrom.Properties.MaxValue = Today
            dteToDate.Properties.MaxValue = Today
            dteFromDate.EditValue = Format(DateAdd(DateInterval.Month, (-1), Today), gstrDtFormat)
            dteToDate.EditValue = Format(Today, gstrDtFormat)
            dteMnthFrom.EditValue = Format(DateAdd(DateInterval.Month, (-1), Today), gstrDtFormat)
            dteMnthTo.EditValue = Format(Today, gstrDtFormat)
            Call PopulateComboDX(cboFromYear, "tblVisits", "distinct Year(VisitDate) [VisitDate]", "VisitDate Is Not Null", "VisitDate")
            Call PopulateComboDX(cboToYear, "tblVisits", "distinct Year(VisitDate) [VisitDate]", "VisitDate Is Not Null", "VisitDate")
            Me.Text = mstrReportType
            If mstrReportType = "Revenue By Date" Then
                grbDate.Visible = True
                grbDiagnosis.Visible = False
                grbMonthly.Visible = False
            ElseIf mstrReportType = "Revenue By Diagnosis" Then
                grbDate.Visible = False
                grbDiagnosis.Visible = True
                grbMonthly.Visible = False
            ElseIf mstrReportType = "Revenue By Month" Then
                grbDate.Visible = False
                grbDiagnosis.Visible = False
                grbMonthly.Visible = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnViewReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        Try
            If IsValidEntry() = False Then Exit Sub
            Dim strQuery As String = ""
            If mstrReportType = "Revenue By Date" Then
                'Call ShowPatientStatementReport(lkDiagnosis.EditValue, dteFromDate.EditValue, dteToDate.EditValue, chkViewAll.Checked, Me.ParentForm)
                strQuery = "SELECT PaymentDate,Sum(Payment) as [Amount] from tblPayment Where PaymentDate Between '" & Format(CDate(dteMnthFrom.EditValue), gstrDtFormat) & "' and '" & Format(CDate(dteMnthTo.EditValue), gstrDtFormat) & "' Group By PaymentDate"
                Call ShowRevenueReport(strQuery, Me.ParentForm, "Revenue By Date", "From " & Format(CDate(dteMnthFrom.EditValue), gstrDtFormat) & " To " & Format(CDate(dteMnthTo.EditValue), gstrDtFormat))
            ElseIf mstrReportType = "Revenue By Month" Then
                Dim dtFrm, dtTo As DateTime
                dtFrm = GetFirstDateOfMonth((cboFromMonth.SelectedIndex + 1), cboFromYear.Text)
                dtTo = GetLastDateOfMonth((cboToMonth.SelectedIndex + 1), cboToYear.Text)
                'Dim dsTemp As DataSet = FillDataSet("SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(" & cboMonth.Text & ")-1)," & cboMonth.Text & "),101) [From Date],CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1," & cboMonth.Text & "))),DATEADD(mm,1," & cboMonth.Text & ")),101) [To Date]")
                'With dsTemp.Tables(0)
                '    dtFrm = Format(CDate(.Rows(0).Item("From Date")), gstrDtFormat)
                '    dtTo = Format(CDate(.Rows(0).Item("To Date")), gstrDtFormat)
                'End With
                strQuery = "SELECT datename(month,PaymentDate) as [PaymentDate],Sum(Payment) as [Amount] from tblPayment Where PaymentDate Between '" & Format(dtFrm, "MM/dd/yyyy") & "' and '" & Format(dtTo, "MM/dd/yyyy") & "' Group By datename(month,PaymentDate)"
                Call ShowRevenueReport(strQuery, Me.ParentForm, "Revenue By Month")
            ElseIf mstrReportType = "Revenue By Diagnosis" Then
                Call ShowGrowthReport(lkDiagnosis.EditValue, "tblLengthForAge", "Age", "Height", "", "RptLengthGrowthChart.rpt", "Growth Chart", Me.ParentForm)
            ElseIf mstrReportType = "Revenue By Demographics" Then
                Call ShowGrowthReport(lkDiagnosis.EditValue, "tblWeightForAge", "Age", "Weight", "", "RptWeightGrowthChart.rpt", "Growth Chart", Me.ParentForm)
            End If
            'Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub glkPatient_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkDiagnosis.ButtonClick
        Try
            If e.Button.Index = 1 Then
                Call PopulateLookUpName()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        IsValidEntry = False

        If mstrReportType = "Revenue By Diagnosis" AndAlso IsDBNullOrNothing(lkDiagnosis.EditValue) Then
            strMsg = "Please select Diagnosis"
            lkDiagnosis.Focus()
        ElseIf mstrReportType = "Revenue By Diagnosis" AndAlso chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteFromDate.EditValue) Then
            strMsg = "Please select From Date"
            dteFromDate.Focus()
        ElseIf mstrReportType = "Revenue By Diagnosis" AndAlso chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteToDate.EditValue) Then
            strMsg = "Please select To Date"
            dteToDate.Focus()
        ElseIf mstrReportType = "Revenue By Month" AndAlso cboFromMonth.SelectedIndex < 0 Then
            strMsg = "Please select From Month"
            cboFromMonth.Focus()
        ElseIf mstrReportType = "Revenue By Month" AndAlso cboFromYear.SelectedIndex < 0 Then
            strMsg = "Please select From Year"
            cboFromYear.Focus()
        ElseIf mstrReportType = "Revenue By Month" AndAlso cboToMonth.SelectedIndex < 0 Then
            strMsg = "Please select To Month"
            cboToMonth.Focus()
        ElseIf mstrReportType = "Revenue By Month" AndAlso cboToYear.SelectedIndex < 0 Then
            strMsg = "Please select To Year"
            cboToYear.Focus()
        End If

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Sub PopulateLookUpName()
        Dim dsTemp As DataSet

        dsTemp = FillDataSet("SELECT LtblDiagnosis.DiagnosisId [DiagnosisId],LtblDiagnosis.Diagnosis [Diagnosis], " & _
                             "LtblDiagnosisGroup.DiagnosisGroup from LtblDiagnosis INNER JOIN " & _
                             "LtblDiagnosisGroup ON LtblDiagnosis.DiagnosisGroupId=LtblDiagnosisGroup.DiagnosisGroupID")
        With lkDiagnosis.Properties
            .DataSource = dsTemp.Tables(0)
            .DisplayMember = "Diagnosis"
            .ValueMember = "DiagnosisId"
            .NullText = ""
            .ShowFooter = False
            .View.OptionsSelection.EnableAppearanceFocusedCell = False
            .View.OptionsView.AutoCalcPreviewLineCount = True
            .View.OptionsFilter.AllowColumnMRUFilterList = True
            .View.OptionsFilter.AllowFilterEditor = True
            .View.OptionsFilter.AllowMRUFilterList = True
            .View.OptionsView.ShowAutoFilterRow = True
            .View.OptionsView.ShowIndicator = False
            .View.OptionsView.ShowGroupPanel = False
            .View.OptionsView.ShowPreview = True
            '.View.OptionsFilter.ShowAllTableValuesInFilterPopup = True
            '.ImmediatePopup = True
            .ShowFooter = False
            .View.Columns("DiagnosisId").Visible = False
        End With
    End Sub

    Private Sub chkViewAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkViewAll.CheckedChanged
        If chkViewAll.Checked Then
            dteFromDate.Enabled = False
            dteToDate.Enabled = False
        Else
            dteFromDate.Enabled = True
            dteToDate.Enabled = True
        End If
    End Sub

    Private Function GetFirstDateOfMonth(ByVal iMonth As Integer, ByVal iYear As Integer)
        Dim dtFrom As DateTime = New DateTime(iYear, iMonth, 1)
        dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1))
        Return dtFrom
    End Function

    Private Function GetLastDateOfMonth(ByVal iMonth As Integer, ByVal iYear As Integer)
        Dim dtTo As New DateTime(iYear, iMonth, 1)
        dtTo = dtTo.AddMonths(1)
        dtTo = dtTo.AddDays(-(dtTo.Day))
        Return dtTo
    End Function

End Class
